﻿using System;
using System.Collections;
using System.Collections.Generic;
using Table.Blo;
using UnityEngine;
using Yunchang;

namespace AutoChess
{
    /*public class HeroUnitRenderer : BaseRenderer
    {
        //关联的英雄逻辑实体
        public ACHero heroUnit;
        private HeroBehaviourCtr heroCtr;//行为状态机
        private CellModelLoadHelper cellModelLoadHelper;  //模型加载帮助类
        private AutoChessHeroInfoUI sceneUI;//场景UI

        private CommonEffect starEffect;
        private GameObject modelParent;

        private bool isPreviewModel;
        public GameObject ModelParent
        {
            get
            {
                if (modelParent == null)
                {
                    modelParent = new GameObject("ModelRoot");
                    modelParent.transform.SetParent(transform);
                    modelParent.transform.Normalize();
                }
                return modelParent;
            }
        }
        #region Behaviour

        //模型只需要在英雄渲染器 销毁的时候进行卸载
        protected override void OnRendererDestroy()
        {
            cellModelLoadHelper.UnloadModel();
            cellModelLoadHelper = null;
        }
        protected override void OnRendererUpdate()
        {
            if (cellModelLoadHelper != null)
                cellModelLoadHelper.Update();

            if (heroCtr != null)
                heroCtr.UpdateState();
        }
        //链接一个英雄 并且刷新渲染
        public override void OnConnect(ACHero data)
        {
            var temp = data as ACHero;
            this.heroUnit = temp;
            if (heroUnit != null)
            {
                gameObject.SetActive(true);
                transform.name = heroUnit.ToString();
                RefreshHeroSceneUI();                   //刷新跟随英雄的场景UI
                SetModelActiveState(true);
            }
            heroCtr = new HeroBehaviourCtr(this);//渲染行为状态机
            heroCtr.SwitchState(HeroBehaviourState.None);
            if (cellModelLoadHelper == null)
            {
                cellModelLoadHelper = new CellModelLoadHelper(ModelParent.transform, AutoChessFieldViewCtr.Ins.LoadEventManager, "town",
                    null, false);
            }
            else
            {
                cellModelLoadHelper.SetOnLoadComplete(null);
            }
            cellModelLoadHelper.ResetModelLayer(LayerName.SceneActor);
            if (heroUnit != null)
            {
                RefreshModelTransform(heroUnit);        //更新模型偏移大小 商店UI 还是场景
                UpdatePosition(heroUnit.MapGrid.Position, heroUnit.MapGrid.GridType);//更新位置
            }
        }
        public void PlayEnterFieldEffect()
        {
            if (heroUnit.MapGrid.GridType == GridType.EnemyGrid)//对手的话播放 入场特效
            {
                CommonEffectManager.Instance.PlayEffectImmediate(CommonEffectResId.AC_Hero_Enter_Field, transform);
            }
        }
        //断开渲染的实体的时候 放入缓存池中
        public override void OnDisconnect()
        {
            //如果是UI上的英雄渲染器 放回到节点
            if (heroUnit != null && heroUnit.MapGrid.GridType == GridType.ShopGrid)
            {
                cellModelLoadHelper.ResetModelLayer(LayerName.SceneActor);
                transform.SetParent(AutoChessFieldViewCtr.Ins.HeroUnitRoot);
                transform.Normalize();
            }
            heroCtr = null;
            heroUnit = null;
            if (starEffect != null)
                starEffect.Clear();
            starEffect = null;
            //TODO 放入池中
            transform.SetUnused(false, GameConst.AC_HERO);
        }
        public void OnUpStarBuff()
        {
            Invoke("_OnUpStarBuff", AutoChessFieldViewCtr.Ins.SceneConfig.showStarBuff);
        }
        private void _OnUpStarBuff()
        {
            if (sceneUI != null)
            {
                sceneUI.RefreshStarByChessId(heroUnit.ChessID);
            }
            AutoChessManager.Ins.ShotStarUpgradeEffect(heroUnit.MapGrid);
        }
        public void OnOffStarBuff()
        {
            if (sceneUI != null)
            {
                sceneUI.RefreshStarByChessId(heroUnit.ChessID);
            }
        }
        public override void RefreshRenderer()
        {
            //刷新显示
            if (heroUnit != null)
            {
                //显示升星UI
                if (sceneUI != null)
                {
                    sceneUI.upStarBtn.SetActive(heroUnit.isShowUpStar);
                }

                if (isPreviewModel)
                {
                    if (starEffect != null)
                        starEffect.GetEffectHolder().gameObject.SetActive(false);
                }
                else
                {
                    //可升星的特效
                    if (heroUnit.HeroEffectType == HeroEffectType.Field3Chess &&
                        (heroUnit.MapGrid.GridType == GridType.FightGrid || heroUnit.MapGrid.GridType == GridType.PreChooseGrid))
                    {
                        if (starEffect == null)
                            starEffect = UtilityHelper.PlayEffect(CommonEffectResId.AC_Hero_Scene_Upgrade, ModelParent.transform);
                        else
                            starEffect.GetEffectHolder().gameObject.SetActive(true);
                    }
                    else
                    {
                        if (starEffect != null)
                            starEffect.GetEffectHolder().gameObject.SetActive(false);
                    }
                }

                UpdatePosition(heroUnit.MapGrid.Position, heroUnit.MapGrid.GridType);
            }
        }
        #endregion

        #region anim
        public void PlayAnimation(string anim, float speed = 1)
        {
            cellModelLoadHelper.PlayCrossFadeAnim(anim);
            cellModelLoadHelper.PlayAnimatorSpeed(speed);
        }
        public float GetAnimationTime(string anim)
        {
            return cellModelLoadHelper.GetAnimtionTime(anim);
        }
        public void SetAnimSpeed(float speed)
        {
            cellModelLoadHelper.PlayAnimatorSpeed(speed);
        }
        public bool IsCanPlayAnim()
        {
            return cellModelLoadHelper != null && cellModelLoadHelper.IsCanPlayAnim();
        }
        #endregion

        public void SetModelActiveState(bool active)
        {
            ModelParent.SetActive(active);
            if (sceneUI != null)
            {
                sceneUI.gameObject.SetActive(active);
            }
        }

        public void JumpToTarget(Vector3 targetPos, JumpType jumpType, System.Action jumpEnd)
        {
            if (heroCtr != null)
                heroCtr.JumpToPosition(new JumpPakage(targetPos, jumpType, jumpEnd));
            else
                Debug.LogWarning("逻辑异常!heroCtr=null");
        }

        public void ShowUp(System.Action end)
        {
            if (heroCtr != null)
                heroCtr.ShowUp(new ShowUpPakage(end));
            else
                Debug.LogWarning("逻辑异常!heroCtr=null");
        }

        public void Show(string animName, Action end)
        {
            if (heroCtr != null)
                heroCtr.Show(new ShowPackage(animName, end));
            else
                Debug.LogWarning("逻辑异常!heroCtr=null");
        }

        /// <summary>
        /// 设置模型偏移大小
        /// </summary>
        /// <param name="heroUnit"></param>
        private void RefreshModelTransform(ACHero heroUnit)
        {
            LoadModel(heroUnit);
            
            if (heroUnit.MapGrid.GridType == GridType.ShopGrid)
            {
                Vector3 pos, scale, angle;
                AutoChessBlo.GetHeroOffset(heroUnit.HeroID, out pos, out scale, out angle);
                cellModelLoadHelper.SetModelOffset(pos, scale, angle);
            }
            else
            {
                Vector3 scale = Vector3.one;
                var dictModel = Table.Blo.TableSailorModelBlo.GetTableSailorModelByHeroIdAndAppearence(heroUnit.HeroID, 0);
                if (dictModel != null)
                {
                    //设置模型偏移等默认值
                    if (dictModel != null)
                    {
                        scale *= 0.85f * (float)dictModel.ModelScale * AutoChessBlo.GetFieldScale(heroUnit.HeroID);
                    }
                }
                float angle = heroUnit.MapGrid.GridType == GridType.PreChooseGrid ? 90 : 0;
                if (heroUnit.MapGrid.GridType == GridType.PreChooseGrid)
                {
                    angle = 180;
                }
                else if (heroUnit.MapGrid.GridType == GridType.FightGrid)
                {
                    angle = 90;
                }
                else if (heroUnit.MapGrid.GridType == GridType.EnemyGrid)
                {
                    angle = -90;
                }
                cellModelLoadHelper.SetModelOffset(Vector3.zero, scale, new Vector3(0, angle, 0));
            }
        }

        //如果是跳到UI格子上 就把模型节点帮到UI格子上,并且改下layer
        private void RefreshHeroRootInGrid()
        {
            if (heroUnit.MapGrid.GridType == GridType.ShopGrid)
            {
                cellModelLoadHelper.ResetModelLayer(LayerName.UI);
                if (heroUnit.MapGrid.gridUnitRender != null)
                {
                    //  Debug.LogWarning("一个英雄在格子商店上");
                    transform.SetParent(heroUnit.MapGrid.gridUnitRender.transform);
                    transform.Normalize();
                }
                else
                {
                    Debug.LogWarning("再一个格子没有连接 渲染器的时候调用了 渲染器逻辑");
                }
            }
        }

        //刷新英雄 跟随UI
        private void RefreshHeroSceneUI()
        {
            if (heroUnit.MapGrid.GridType == GridType.ShopGrid)
            {
                if (sceneUI != null)
                {
                    sceneUI.Close();
                    sceneUI = null;
                }
                return;
            }
            AutoChessHeroArgs args = new AutoChessHeroArgs() { logicObj = heroUnit };
            sceneUI = SceneUIManager.Instance.OpenUI(transform, true, "AutoChessHeroInfoUI", args) as AutoChessHeroInfoUI;
        }

        public void UpdatePosition(Vector3 position, GridType gridType)
        {
            if (heroUnit != null)
            {
                //如果是在商店的格子 位置设置就是把他直接放到 UI格子下面
                if (gridType == GridType.ShopGrid)
                {
                    RefreshHeroRootInGrid();
                }
                else
                {
                    transform.position = position;
                }
            }
        }

        public void UpdateRotation(float angle)
        {
            cellModelLoadHelper.SetModelAngle(new Vector3(0, angle, 0));
        }

        public void UpdatePosition(ACGrid fromGrid, ACGrid toGrid, bool refreshModelTransform = true)
        {
            if (heroUnit != null)
            {
                if (fromGrid == null || fromGrid.GridType == GridType.ShopGrid)
                {
                    transform.position = toGrid.Position;
                    if (refreshModelTransform)
                    {
                        AutoChessManager.Ins.ShotFootPrintEffect(toGrid);
                    }
                }
                else
                {
                    transform.position = toGrid.Position;
                    AutoChessManager.Ins.ShotFootPrintEffect(toGrid);
                    if (toGrid.GridType == GridType.FightGrid)
                        AutoChessManager.Ins.CurrentAutoChessField.AutoUpgradeStarMsg(toGrid);
                    ShowUp(null);
                }
                if (refreshModelTransform)
                    RefreshModelTransform(heroUnit);
            }
        }
        #region drag
        //链接一个英雄
        public void OnConnectPreviewModel(ACHero heroUnit)
        {
            isPreviewModel = true;
            heroCtr = new HeroBehaviourCtr(this);//渲染行为状态机
            heroCtr.SwitchState(HeroBehaviourState.None);
            if (cellModelLoadHelper == null)
                cellModelLoadHelper = new CellModelLoadHelper(ModelParent.transform, AutoChessFieldViewCtr.Ins.LoadEventManager, "town", () => { heroCtr.SwitchState(HeroBehaviourState.Idle); }, false);
            else
                cellModelLoadHelper.SetOnLoadComplete(() => { heroCtr.SwitchState(HeroBehaviourState.Idle); });
            cellModelLoadHelper.ResetModelLayer(LayerName.SceneActor);
            this.heroUnit = heroUnit;
            if (heroUnit != null)
            {
                SetModelActiveState(true);
                transform.name = heroUnit.ToString();

                LoadModel(heroUnit);

                Vector3 scale = Vector3.one;
                var dictModel = Table.Blo.TableSailorModelBlo.GetTableSailorModelByHeroIdAndAppearence(heroUnit.HeroID, 0);
                if (dictModel != null)
                {
                    scale *= 0.85f * (float)dictModel.ModelScale * AutoChessBlo.GetFieldScale(heroUnit.HeroID);
                }
                cellModelLoadHelper.SetModelOffset(Vector3.zero, scale, new Vector3(0, 90, 0));

                UpdatePosition(heroUnit.MapGrid.Position, GridType.FightGrid);
                gameObject.SetActive(true);
            }

            cellModelLoadHelper.SetTransparency(0.3f);

            if (sceneUI != null)
                sceneUI.HideUI();
        }

        private void LoadModel(ACHero heroUnit)
        {
            var guid = heroUnit.playerEntity.playerGuid;
            int fashtionId = 0;
            if (guid != 0)
            {
               fashtionId = AutoChessManager.Ins.cacheData.GetPlayerHeroFathionId(guid,heroUnit.HeroID);
            }
            cellModelLoadHelper.SetData(heroUnit.HeroID, fashtionId);
            cellModelLoadHelper.LoadModel();
        }

        public void OnDisconnectPreviewModel()
        {
            isPreviewModel = false;
            heroUnit = null;
            heroCtr = null;
            transform.SetUnused(false, GameConst.AC_HERO);

            cellModelLoadHelper.SetTransparency(1f);
        }
        #endregion

    }*/
}
